<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>user edit</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/static/super_cmdb/css/font.css">
    <link rel="stylesheet" href="/static/super_cmdb/css/xadmin.css">

  </head>
  
  <body>
    <div class="x-body">
        <form class="layui-form">
          <div class="layui-form-item">
              <label for="user" class="layui-form-label">
                  <span class="x-red">*</span>用户名
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="user" name="user" required="" lay-verify="nikename" value="{{ user_info.name }}"
                  autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="x-red">*</span>将会成为用户唯一的登入名
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_pass" class="layui-form-label">
                  <span class="x-red">*</span>密码
              </label>
              <div class="layui-input-inline">
                  <input type="password" id="L_pass" name="pass" required="" lay-verify="pass" value="{{ user_info.password }}"
                  autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="x-red">*</span>6到16个字符
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
                  <span class="x-red">*</span>确认密码
              </label>
              <div class="layui-input-inline">
                  <input type="password" id="L_repass" name="repass" required="" lay-verify="repass" value="{{ user_info.password }}"
                  autocomplete="off" class="layui-input">
              </div>
          </div>
          {% if current_user_type_id == 1 %}
            <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label">
                  <span class="x-red">*</span>用户类型
              </label>
              <div class="layui-input-inline">
                  <select id="usertype_id" name="usertype_id">
                        <option value="{{ user_type_obj.id }}">{{ user_type_obj.name }}</option>
                        {% for user_type in user_type_list %}
                            <option value="{{ user_type.id }}">{{ user_type.name }}</option>
                        {% endfor %}
                  </select>
              </div>
            </div>
          {% endif %}

            <div class="layui-form-item">
              <label for="phone" class="layui-form-label">
                  <span class="x-red">*</span>联系号码
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="phone" name="phone" required="" lay-verify="nikename" value="{{ user_info.phone }}"
                  autocomplete="off" class="layui-input">
              </div>
            </div>

            <div class="layui-form-item">
              <label for="phone_bak" class="layui-form-label">
                  <span class="x-red">*</span>备用号码
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="phone_bak" name="phone_bak" required="" lay-verify="nikename" value="{{ user_info.phone_bak }}"
                  autocomplete="off" class="layui-input">
              </div>
            </div>

            <div class="layui-form-item">
              <label for="motto" class="layui-form-label">
                  <span class="x-red">*</span>座右铭
              </label>
              <div class="layui-input-inline">
{#                  <input type="text" id="motto" name="motto" required="" lay-verify="nikename" value="{{ user_info.motto }}" autocomplete="off" class="layui-input">#}
                  <textarea id="motto" name="motto" style="margin: 0px;width: 400px;height: 38px;">{{ user_info.hobby }}</textarea>
              </div>
            </div>

            <div class="layui-form-item">
              <label for="hobby" class="layui-form-label">
                  <span class="x-red">*</span>兴趣爱好
              </label>
              <div class="layui-input-inline">
                  <textarea id="hobby" name="hobby" style="margin: 0px;width: 400px;height: 80px;">{{ user_info.hobby }}</textarea>
              </div>
            </div>

          <div class="layui-form-item" >
              <label for="L_repass" class="layui-form-label">
              </label>
              <button id="my_edit" class="layui-btn" lay-filter="edit" lay-submit="">
                  确认修改
              </button>
          </div>
      </form>
    </div>
    <script type="text/javascript" src="/static/super_cmdb/js/jquery.min.js"></script>
    <script type="text/javascript" src="/static/super_cmdb/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/super_cmdb/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/super_cmdb/js/jquery.cookie.js"></script>
    <script>

        $(function () {
            $.ajaxSetup({
                beforeSend:function (xhr,settings) {
                    xhr.setRequestHeader("X-CSRFtoken",$.cookie("csrftoken"));
                }
            })
        });

      layui.use(['form','layer'], function(){
          $ = layui.jquery;
        var form = layui.form
        ,layer = layui.layer;

        //自定义验证规则
        form.verify({
          nikename: function(value){
            if(value.length < 1){
              return '昵称至少得1个字符啊';
            }
          }
          ,pass: [/(.+){6,12}$/, '密码必须6到12位']
          ,repass: function(value){
              if($('#L_pass').val()!=$('#L_repass').val()){
                  return '两次密码不一致';
              }
          }
        });


        //监听提交
        form.on('submit(edit)', function(data){
          console.log(data);
          //发异步，把数据提交给php
            $.ajax({
                    url: "/super_cmdb/user_edit-{{ user_info.id }}/",
                    type: "POST",
                    //data: $('#layui-form').serialize(),
                    data: {
                        "user":$("#user").val(),
                        "password":$("#L_pass").val(),
                        "usertype_id":$("#usertype_id").val(),
                        "phone":$("#phone").val(),
                        "phone_bak":$("#phone_bak").val(),
                        "motto":$("#motto").val(),
                        "hobby":$("#hobby").val(),
                    },
                    success: function (data) {
                        console.log(data);
                    }
                });

          layer.alert("编辑成功", {icon: 6},function () {
              // 获得frame索引
              var index = parent.layer.getFrameIndex(window.name);
              //关闭当前frame
              parent.layer.close(index);
          });
          return false;
        });


      });

  </script>
  </body>

</html>